﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CvModel;
using System.Data.Entity.Migrations;
using System.Data.SqlClient;
using System.Data;

namespace DAL
{
    public class ApplicantDB
    {
        private static CvModelContainer _context = new CvModelContainer();

        //CREATE
        public static int CreateApplicant(Applicant applicant)
        {
            try
            {
                var query = _context.Applicant.Add(applicant);
                return _context.SaveChanges();
            }
            catch (Exception)
            {

                throw;
            }
        }
        //READ
        public static Applicant GetApplicant(string personNumber, string password)
        {
            try
            {

                var query = _context.Applicant.SingleOrDefault(a => a.Password == password && a.PersonNumber == personNumber);
                var newUser = new Applicant
                {
                    Address = query.Address,
                    ApplicantId = query.ApplicantId,
                    City = query.City,
                    Cv = query.Cv,
                    Email = query.Email,
                    FirstName = query.FirstName,
                    IsDeleted = query.IsDeleted,
                    LastName = query.LastName,
                    PersonNumber = query.PersonNumber,
                    Zip = query.Zip,
                    Phone = query.Phone,
                    RegistredCv = query.RegistredCv
                };

                return newUser;


            }
            catch (Exception)
            {

                throw;
            }
        }

        public static Applicant GetApplicantOnId(int applicantId)
        {
            try
            {
                var query = _context.Applicant.SingleOrDefault(a => a.ApplicantId == applicantId);
                var newUser = new Applicant
                {
                    Address = query.Address,
                    ApplicantId = query.ApplicantId,
                    City = query.City,
                    Cv = query.Cv,
                    Email = query.Email,
                    FirstName = query.FirstName,
                    IsDeleted = query.IsDeleted,
                    LastName = query.LastName,
                    PersonNumber = query.PersonNumber,
                    Zip = query.Zip,
                    Phone = query.Phone,
                    RegistredCv = query.RegistredCv
                };
                return newUser;
            }
            catch (Exception)
            {
                throw;
            }
        }

        public static Applicant GetAllpApplicantOnIdInclPassword(int applicantId)
        {
            return _context.Applicant.SingleOrDefault(a => a.ApplicantId == applicantId);
        }

        public static int GetApplicantId(string personNumber, string password)
        {
            try
            {
                var query = _context.Applicant
                    .Where(a => a.PersonNumber == personNumber && a.Password == password)
                    .Select(a => a.ApplicantId).ToList().First();

                return query;
            }
            catch (Exception)
            {

                throw;
            }
        }

        public static bool ApplicantIsLoggedIn(string personNumber, string password)
        {
            try
            {

                var query = _context.Applicant.Where(a => a.PersonNumber == personNumber && a.Password == password && a.IsDeleted != true).ToList();
                if (query.Count > 0)
                {
                    if (query != null)
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
                else
                {
                    return false;
                }
                
                
            }
            catch (Exception)
            {
                throw;
            }
        }

        public static List<Applicant> GetApplicants()
        {
            try
            {
                var query = _context.Applicant.Select(a => a).ToList();
                return query;

            }
            catch (Exception)
            {

                throw;
            }
        }
        //UPDATE
        public static int UpdateApplicant(Applicant applicant)
        {
            try
            {
                _context.Applicant.AddOrUpdate(applicant);
                return _context.SaveChanges();
            }
            catch (Exception)
            {

                throw;
            }
        }
        //DELETE
        public static int DeleteApplicant(int applicantId)
        {
            try
            {
                var query =
                    (from a in _context.Applicant
                     where a.ApplicantId == applicantId
                     select a).Single();

                _context.Applicant.Remove(query);

                return _context.SaveChanges();
            }
            catch (Exception)
            {
                throw;
            }
        }
    }
}
